﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace ProjScan.Submodules.Shader_Analysis
{
	// Token: 0x02000185 RID: 389
	internal class ShaderPassProcessor
	{
		// Token: 0x06000D73 RID: 3443 RVA: 0x0003BBE0 File Offset: 0x00039DE0
		public void ProcessShaderData(ShaderPassKeyManager shaderPassKeyManager)
		{
			string[] array = File.ReadAllLines(shaderPassKeyManager.path);
			string[] separator = new string[]
			{
				" "
			};
			SAShaderPass sashaderPass = new SAShaderPass();
			SAShaderPass sashaderPass2 = null;
			int num = -1;
			bool flag = false;
			SAKeywordLine sakeywordLine = new SAKeywordLine();
			HashSet<char> hashSet = new HashSet<char>();
			hashSet.Add(' ');
			hashSet.Add('\t');
			hashSet.Add('\r');
			hashSet.Add('\n');
			HashSet<char> hashSet2 = hashSet;
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				bool flag2 = text.Length != 0;
				if (flag2)
				{
					int num2 = text.IndexOf("//");
					bool flag3 = num2 >= 0;
					if (flag3)
					{
						text = text.Substring(0, num2);
					}
					bool flag4 = text.Length != 0;
					if (flag4)
					{
						bool flag5 = false;
						int num3 = text.IndexOf("/*");
						int num4 = text.IndexOf("*/");
						bool flag6 = num3 > 0 && (num2 > num3 || num2 < 0);
						if (flag6)
						{
							flag = true;
							flag5 = true;
							text = text.Substring(0, num3);
						}
						bool flag7 = num4 > num3 && (num2 > num4 || num2 < 0);
						if (flag7)
						{
							flag = false;
							bool flag8 = !flag5;
							if (flag8)
							{
								text = text.Substring(num4 + 2);
							}
						}
						bool flag9 = text.Length != 0 && (flag5 || !flag);
						if (flag9)
						{
							string text2 = text.ToUpper();
							bool flag10 = text2.Contains("PASS") && !text2.Contains("GRABPASS") && !text2.Contains("USEPASS");
							if (flag10)
							{
								bool flag11 = false;
								bool flag12 = false;
								for (int j = text2.IndexOf("PASS") + 4; j < text.Length; j++)
								{
									bool flag13 = !hashSet2.Contains(text[j]);
									if (flag13)
									{
										flag11 = (text[j] == '{');
										flag12 = true;
										break;
									}
								}
								int num5 = i + 1;
								while (num5 < array.Length && !flag12)
								{
									string text3 = array[num5];
									int num6 = text3.IndexOf("//");
									bool flag14 = num6 >= 0;
									if (flag14)
									{
										text3 = text3.Substring(0, num6);
									}
									for (int k = 0; k < text3.Length; k++)
									{
										bool flag15 = !hashSet2.Contains(text3[k]);
										if (flag15)
										{
											flag11 = (text3[k] == '{');
											flag12 = true;
											break;
										}
									}
									num5++;
								}
								bool flag16 = flag11;
								if (flag16)
								{
									bool flag17 = num >= 0;
									if (flag17)
									{
										sashaderPass.passNum = num;
										bool flag18 = sashaderPass2 != null;
										if (flag18)
										{
											sashaderPass.Add(sashaderPass2.keywordLines);
										}
										shaderPassKeyManager.AddShaderPass(sashaderPass);
									}
									else
									{
										bool flag19 = sashaderPass.keywordCount > 0;
										if (flag19)
										{
											sashaderPass2 = sashaderPass;
										}
									}
									sashaderPass = new SAShaderPass();
									num++;
								}
							}
							else
							{
								PragmaType pragmaType = PragmaType.Unknown;
								int num7 = 0;
								int num8 = text.IndexOf("#pragma multi_compile ");
								bool flag20 = num8 >= 0;
								if (flag20)
								{
									pragmaType = PragmaType.MultiCompileGlobal;
									num7 = "#pragma multi_compile ".Length;
								}
								else
								{
									num8 = text.IndexOf("#pragma shader_feature ");
									bool flag21 = num8 >= 0;
									if (flag21)
									{
										pragmaType = PragmaType.FeatureGlobal;
										num7 = "#pragma shader_feature ".Length;
									}
									else
									{
										num8 = text.IndexOf("#pragma multi_compile_local ");
										bool flag22 = num8 >= 0;
										if (flag22)
										{
											pragmaType = PragmaType.MultiCompileLocal;
											num7 = "#pragma multi_compile_local ".Length;
										}
										else
										{
											num8 = text.IndexOf("#pragma shader_feature_local ");
											bool flag23 = num8 >= 0;
											if (flag23)
											{
												pragmaType = PragmaType.FeatureLocal;
												num7 = "#pragma shader_feature_local ".Length;
											}
										}
									}
								}
								bool flag24 = num8 >= 0;
								if (flag24)
								{
									SAKeywordLine sakeywordLine2 = new SAKeywordLine();
									sakeywordLine2.pragmaType = pragmaType;
									int num9 = text.IndexOf("//", num8 + num7);
									bool flag25 = num9 < 0;
									if (flag25)
									{
										num9 = text.Length;
									}
									int length = num9 - num8 - num7;
									string[] array2 = text.Substring(num8 + num7, length).Split(separator, StringSplitOptions.RemoveEmptyEntries);
									for (int l = 0; l < array2.Length; l++)
									{
										array2[l] = array2[l].Trim();
									}
									for (int m = 0; m < array2.Length; m++)
									{
										sakeywordLine2.Add(shaderPassKeyManager.GetKeywordByName(array2[m]));
									}
									sashaderPass.Add(sakeywordLine2);
								}
							}
						}
					}
				}
			}
			sashaderPass.passNum = Mathf.Max(num, 0);
			bool flag26 = sashaderPass2 != null;
			if (flag26)
			{
				sashaderPass.Add(sashaderPass2.keywordLines);
			}
			shaderPassKeyManager.AddShaderPass(sashaderPass);
			shaderPassKeyManager.ProcessKeywordLines();
		}

		// Token: 0x04000840 RID: 2112
		public static ShaderPassProcessor I = new ShaderPassProcessor();
	}
}
